package com.zzpct.wh.mypostmna;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.zzpct.wh.mypostmna.entity.Cars;
import com.zzpct.wh.mypostmna.entity.Cheliangpingpai;
import com.zzpct.wh.mypostmna.service.CarsService;
import com.zzpct.wh.mypostmna.service.CheliangpingpaiService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //getAllCarsInfo();
        getCheliangpinpaiInfo();
    }

    private void getCheliangpinpaiInfo() {
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://apis.juhe.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
        CheliangpingpaiService cheliangpingpaiService=retrofit.create(CheliangpingpaiService.class);
        Call<Cheliangpingpai>cheliangpingpaiCall=cheliangpingpaiService.getAllpingpai();
        cheliangpingpaiCall.enqueue((new Callback<Cheliangpingpai>() {
            @Override
            public void onResponse(Call<Cheliangpingpai> call, Response<Cheliangpingpai> response) {
               Cheliangpingpai cheliangpingpai=response.body();
                Log.d(TAG, "onResponse: "+new Gson().toJson(cheliangpingpai));

            }

            @Override
            public void onFailure(Call<Cheliangpingpai> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        }));

    }

    private void getAllCarsInfo() {
        //创建retrofit对象
        Retrofit retrofit= new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建接口对象
        CarsService carsService=retrofit.create(CarsService.class);
        //调用接口中的抽象方法
        Call<Cars>allCars=carsService.getAllCars();
        //调用Call类型中的异步方法
        allCars.enqueue(new Callback<Cars>() {
            @Override
            public void onResponse(Call<Cars> call, Response<Cars> response) {
                Cars cars=response.body();
                Log.d(TAG, "onResponse: "+new Gson().toJson(cars));
            }

            @Override
            public void onFailure(Call<Cars> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });


    }
}
